#ifndef __I2C_SLAVE_H
#define __I2C_SLAVE_H

#include "main.h"

typedef enum 
{
		I2C_CMD_IDLE, 
		I2C_SLAVE_ADDR, 
		I2C_PUT_DATA, 
		I2C_GET_DATA,
		I2C_STATE_DATA
}iic_cmd_t;

#define CHIPID  0x01
#define REG1    0x0A
#define REG2    0x0B
#define REG3    0x0C
#define REG4    0x0D
#define REG5    0x0E

#define LASTREG 0xFF


typedef struct i2c_slave_init_t i2c_slave_init_t;

struct i2c_slave_init_t{
  I2C_HandleTypeDef *hi2c;
  void (*error_handler)();
};

struct i2c_slave_data_t{
  iic_cmd_t i2cs_state ;
	uint8_t i2cs_register;
  uint8_t i2cs_rxbuf[2];
  uint8_t i2cs_txbuf[2];
  uint8_t i2cs_memory[0xFF];
};
extern struct i2c_slave_data_t i2c_data;
extern void (*_error_handler)();

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c);
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c);
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c);
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t direction, uint16_t addrMatchCode);
void I2C_Slave_Init(i2c_slave_init_t *i2c_init);
void I2C_Error_Handler(void);
void I2C_Slave_Set_Register(uint8_t reg, uint8_t data);
uint8_t I2C_Slave_Get_Register(uint8_t reg);

#endif

